package cn.iocoder.yudao.module.book.dal.mysql.community;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.dal.dataobject.community.PostCommentsDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * 动态评论 Mapper
 *
 * @author system
 */
@Mapper
public interface PostCommentsMapper extends BaseMapperX<PostCommentsDO> {

    /**
     * 分页查询评论列表
     */
    default PageResult<PostCommentsDO> selectCommentsPage(Long postId, Integer page, Integer pageSize) {
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(page);
        pageParam.setPageSize(pageSize);
        return selectPage(pageParam, new LambdaQueryWrapperX<PostCommentsDO>()
                .eq(PostCommentsDO::getPostId, postId)
                .eq(PostCommentsDO::getStatus, 1) // 只查询正常状态的评论
                .isNull(PostCommentsDO::getParentId) // 只查询顶级评论
                .orderByDesc(PostCommentsDO::getCreateTime));
    }

    /**
     * 查询回复列表
     */
    default List<PostCommentsDO> selectRepliesByParentId(Long parentId) {
        return selectList(new LambdaQueryWrapperX<PostCommentsDO>()
                .eq(PostCommentsDO::getParentId, parentId)
                .eq(PostCommentsDO::getStatus, 1)
                .orderByAsc(PostCommentsDO::getCreateTime));
    }

    /**
     * 更新点赞数
     */
    int updateLikesCount(@Param("commentId") Long commentId, @Param("increment") Integer increment);

    /**
     * 根据动态ID统计评论数
     */
    default Long countByPostId(Long postId) {
        return selectCount(new LambdaQueryWrapperX<PostCommentsDO>()
                .eq(PostCommentsDO::getPostId, postId)
                .eq(PostCommentsDO::getStatus, 1));
    }

}
